package veterinaria.negocio;

import java.util.Collection;
import veterinaria.dao.DoctorDAO;
import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Doctor;
import veterinaria.modelo.Persona;
import veterinaria.negocio.GestionPersona;
public class GestionDoctor {

	public Collection<Doctor> buscarPorNombre(String pNom) throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		return dao.buscarPorNombre(pNom);
	}

	public Doctor insertar(String pAdm, String pUsu, Persona pPer) throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		GestionPersona gPer= new GestionPersona();
		String pNom = pPer.getPerNombre();
		String pDom=pPer.getPerDomicilio();
		String pTel=pPer.getPerTelefono();
		String pCel=pPer.getPerCelular();
		String pCor=pPer.getPerCorreo();
		String pIma=pPer.getPerImagen();
		Persona dPer=gPer.insertar(pNom, pDom, pTel, pCel, pCor, pIma);
		Doctor cReg = new Doctor();
		String cUsu=busUsuario(pUsu);
		if (cUsu==null) {
			cReg.setDocAdmin(pAdm);
			cReg.setUsuCodigo(pUsu);
			cReg.setPersona(dPer);
			return dao.insertar(cReg);			
		}				
		else{
			System.out.println("Usuario ya registrado : "+pUsu);
			return dao.obtener(0);						
		}			
	}

	public Doctor obtener(int pCod) throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		return dao.obtener(pCod);
	}

	public void eliminar(int pCod) throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		dao.eliminar(pCod);
	}

	public Doctor actualizar(int pCod, String pAdm, String pUsu, Persona pPer) throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		Doctor cReg = new Doctor();
		cReg.setDocCodigo(pCod);
		cReg.setDocAdmin(pAdm);
		cReg.setUsuCodigo(pUsu);
        cReg.setPersona(pPer); 
		return dao.actualizar(cReg);
	}
	
	public String busUsuario(String pUsu) throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		return dao.busUsuario(pUsu);
	}
	
	public Collection<Doctor> listar() throws DAOExcepcion {
		DoctorDAO dao = new DoctorDAO();
		return dao.listar();
	}

}